﻿using System;
using System.Diagnostics;
using System.Runtime.Remoting.Messaging;

namespace OperationPlugins
{
    /// <summary>
    /// Identifies a branch (e.g inside domain model).
    /// </summary>
    [DebuggerDisplay("{_value}")]
    [Serializable]
    public sealed class BranchId : MarshalByRefObject, ILogicalThreadAffinative
    {
        private readonly string _value;

        internal BranchId()
        {
        }

        /// <summary>
        /// Initializes a new instance of the BranchId class.
        /// </summary>
        /// <param name="value">The value of BranchId</param>
        public BranchId(string value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            _value = value;
        }

        /// <summary>
        /// Gets the value of BranchId.
        /// </summary>
        public string Value
        {
            get { return _value; }
        }

        public override int GetHashCode()
        {
            if (this == Branches.Any)
            {
                return GetType().FullName.GetHashCode();
            }
            else
            {
                return _value.GetHashCode();
            }
        }

        public override bool Equals(object obj)
        {
            var other = obj as BranchId;
            return other != null && other.GetHashCode() == GetHashCode();
        }

    }
}